<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 10/19/14
 * Time: 6:06 PM
 */

$loggerFactory = new \log\LoggerFactory(array('conf/log.xml'));

//$logger = $loggerFactory->getLoggerByName('browserLogger');
$logger = $loggerFactory->getLoggerByName('systemLogger');

$connectionProperties = new \base\ConnectionProperties();
$connectionProperties->setDbURL(getConfValue("db.url"));
$connectionProperties->setDbName(getConfValue("db.name"));
$connectionProperties->setUsername(getConfValue("db.user"));
$connectionProperties->setPassword(getConfValue("db.pass"));

$transaction = new \mysql\MySQLTransaction();

$dbActions = new \mysql\MySQLDBActions();

global $connection;
$connection = new \base\Connection($transaction, $connectionProperties, $dbActions, $logger);
global $sqlQuery;
$sqlQuery = new \sql\NamedSQLStorage(new SQLParser());


function getSectionTitle($sectionId)
{
  global $connection;
  global $sqlQuery;

  $params = new \sql\Params(":");

  $params->addParam("sectionId", $sectionId);
  $result = $connection->executeQueryInTransaction(new \sql\SimpleSQLQuery($sqlQuery->getQuery("getSectionTitle", $params)));
  $row = mysql_fetch_assoc($result);
  return $row['TITLE'];
}

function  getListRoots($sectionId)
{
  global $connection;
  global $sqlQuery;

  $params = new \sql\Params(":");

  $params->addParam("sectionId", $sectionId);
  return $connection->executeQueryInTransaction(new \sql\SimpleSQLQuery($sqlQuery->getQuery("getListRoots", $params)));

}

function  getListSubs($listRootId)
{
  global $connection;
  global $sqlQuery;

  $params = new \sql\Params(":");

  $params->addParam("listRootId", $listRootId);
  return $connection->executeQueryInTransaction(new \sql\SimpleSQLQuery($sqlQuery->getQuery("getListSubs", $params)));
}

function getAllSections()
{

  global $connection;
  global $sqlQuery;

  return $connection->executeQueryInTransaction(new \sql\SimpleSQLQuery($sqlQuery->getQuery("getSections", null)));
}